using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

[DisallowMultipleComponent]
[RequireComponent(typeof(InstantiatedRoom))]
public class RoomLightingControl : MonoBehaviour
{
    private InstantiatedRoom instantiatedRoom;

    private void Awake()
    {
        // 加载组件
        instantiatedRoom = GetComponent<InstantiatedRoom>();
    }
    
    private void OnEnable()
    {
        //订阅房间更改事件
        StaticEventHandler.OnRoomChanged += StaticEventHandler_OnRoomChanged;
    }

    private void OnDisable()
    {
        //取消订阅房间更改事件
        StaticEventHandler.OnRoomChanged -= StaticEventHandler_OnRoomChanged;
    }
    
    /// <summary>
    /// 处理房间更改事件
    /// </summary>
    private void StaticEventHandler_OnRoomChanged(RoomChangedEventArgs roomChangedEventArgs)
    {
        // 如果这是进入的房间，但房间尚未照亮，则在房间照明中淡入
        if (roomChangedEventArgs.room == instantiatedRoom.room && !instantiatedRoom.room.isLit)
        {
            // 在房间内淡入淡出
            FadeInRoomLighting();
            
            // 确保房间环境装饰游戏对象已激活
            instantiatedRoom.ActivateEnvironmentGameObjects();
            
            // 淡入淡出的环境装饰、游戏对象、照明
            FadeInEnvironmentLighting();

            // 房间门照明淡入淡出
            FadeInDoors();

            instantiatedRoom.room.isLit = true;

        }
    }
    
    /// <summary>
    /// 房间照明淡入淡出
    /// </summary>
    private void FadeInRoomLighting()
    {
        // 淡入房间瓷砖地图的照明
        StartCoroutine(FadeInRoomLightingRoutine(instantiatedRoom));
    }
    
    /// <summary>
    /// 淡入房间照明协程
    /// </summary>
    private IEnumerator FadeInRoomLightingRoutine(InstantiatedRoom instantiatedRoom)
    {
        // 创建新材质以淡入
        Material material = new Material(GameResources.Instance.variableLitShader);

        instantiatedRoom.groundTilemap.GetComponent<TilemapRenderer>().material = material;
        instantiatedRoom.decoration1Tilemap.GetComponent<TilemapRenderer>().material = material;
        instantiatedRoom.decoration2Tilemap.GetComponent<TilemapRenderer>().material = material;
        instantiatedRoom.frontTilemap.GetComponent<TilemapRenderer>().material = material;
        instantiatedRoom.minimapTilemap.GetComponent<TilemapRenderer>().material = material;

        for (float i = 0.05f; i <= 1f; i += Time.deltaTime / Settings.fadeInTime)
        {
            material.SetFloat("Alpha_Slider", i);
            yield return null;
        }

        // 将材质设置回发光材质
        instantiatedRoom.groundTilemap.GetComponent<TilemapRenderer>().material = GameResources.Instance.litMaterial;
        instantiatedRoom.decoration1Tilemap.GetComponent<TilemapRenderer>().material = GameResources.Instance.litMaterial;
        instantiatedRoom.decoration2Tilemap.GetComponent<TilemapRenderer>().material = GameResources.Instance.litMaterial;
        instantiatedRoom.frontTilemap.GetComponent<TilemapRenderer>().material = GameResources.Instance.litMaterial;
        instantiatedRoom.minimapTilemap.GetComponent<TilemapRenderer>().material = GameResources.Instance.litMaterial;
    }
    
    /// <summary>
    /// 淡入环境装饰游戏对象
    /// </summary>
    private void FadeInEnvironmentLighting()
    {
        // 创建新材质以淡入
        Material material = new Material(GameResources.Instance.variableLitShader);

        // 将所有环境组件放在房间中
        Environment[] environmentComponents = GetComponentsInChildren<Environment>();

        // 环通
        foreach (Environment environmentComponent in environmentComponents)
        {
            if (environmentComponent.spriteRenderer != null)
                environmentComponent.spriteRenderer.material = material;
        }

        StartCoroutine(FadeInEnvironmentLightingRoutine(material, environmentComponents));
    }
    
    /// <summary>
    /// 淡入淡出的环境装饰游戏对象协程
    /// </summary>
    private IEnumerator FadeInEnvironmentLightingRoutine(Material material, Environment[] environmentComponents)
    {
        // 在灯光下逐渐淡化
        for (float i = 0.05f; i <= 1f; i += Time.deltaTime / Settings.fadeInTime)
        {
            material.SetFloat("Alpha_Slider", i);
            yield return null;
        }

        // 将环境组件材质设置回光照材质
        foreach (Environment environmentComponent in environmentComponents)
        {
            if (environmentComponent.spriteRenderer != null)
                environmentComponent.spriteRenderer.material = GameResources.Instance.litMaterial;
        }
    }
    
    /// <summary>
    /// 在门中淡入淡出
    /// </summary>
    private void FadeInDoors()
    {
        Door[] doorArray = GetComponentsInChildren<Door>();

        foreach (Door door in doorArray)
        {
            DoorLightingControl doorLightingControl = door.GetComponentInChildren<DoorLightingControl>();

            doorLightingControl.FadeInDoor(door);
        }

    }
}
